package com.ptszyxx.popose.module.main.game;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YVipUtil;
import com.ptszyxx.popose.databinding.FragmentGameDetailBinding;
import com.ptszyxx.popose.module.main.game.vm.GameDetailVM;
import com.stx.xhb.xbanner.XBanner;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.game.GameEntity;
import com.ysg.utils.YImageUtil;
import com.ysg.utils.YStringUtil;
import java.util.List;

public class GameDetailFragment extends BaseFragment<FragmentGameDetailBinding, GameDetailVM> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_game_detail;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public GameDetailVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(GameDetailVM.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if (bundle != null){
            viewModel.entity.set(bundle.getParcelable(BundleConstant.ENTITY));
        }
    }

    @Override
    public void initView() {
        super.initView();
        try {
            if (YStringUtil.isNotEmpty(viewModel.entity.get().getScore())){
                binding.ratingBar1.setRating(Float.valueOf(viewModel.entity.get().getScore()));
                binding.ratingBar2.setRating(Float.valueOf(viewModel.entity.get().getScore()));
                binding.ratingBar3.setRating(Float.valueOf(viewModel.entity.get().getScore()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        setBannerUI();
        setTagUI();
    }


    @Override
    public void initData() {
        super.initData();
        viewModel.requestList(true);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onChatEvent.observe(this, o -> onJumpChat());
    }

    /**
     * 轮播图
     */
    private void setBannerUI() {
        if (viewModel.entity.get() != null && YStringUtil.isNotEmpty(viewModel.entity.get().getTrippic())){
            List<String> list = YStringUtil.getListByComma(viewModel.entity.get().getTrippic());
            binding.banner.setData(list, null);
            binding.banner.loadImage(new XBanner.XBannerAdapter() {
                @Override
                public void loadBanner(XBanner banner, Object model, final View view, int position) {
                    YImageUtil.showRadius((ImageView) view, list.get(position));
                }
            });
            binding.banner.setClipToOutline(true);
        }
    }

    /**
     * 标签
     */
    private void setTagUI() {
        if (viewModel.entity.get() != null && YStringUtil.isNotEmpty(viewModel.entity.get().getTag())){
            List<String> list = YStringUtil.getListByComma(viewModel.entity.get().getTag());
            binding.flowlayout.setData(list, null);
        }
    }

    private void onJumpChat() {
        if (YVipUtil.isVip(getContext(), true)){
            GameEntity entity = viewModel.entity.get();
            if (entity != null){
                YActivityUtil.getInstance().jumpChat(getContext(), entity.getTengxuncode(), entity.getNick(), entity.getPic());
            }
        }
    }
}
